package com.manage.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.manage.demo.dao.model.ExamResults;
import com.manage.demo.service.ResultService;

@Controller
public class ResultController {
	
	@Autowired
	private ResultService ResultService;
	
    @RequestMapping(value="/examresult", method=RequestMethod.GET)
    public String test(HttpServletRequest request, Model model){
    	List<String> alltermlist = ResultService.getTermlist();
    	List<String> termlist = new ArrayList<String>();
    	for(int i = 0; i < alltermlist.size(); i++){  
            if(!termlist.contains(alltermlist.get(i))){  
            	termlist.add(alltermlist.get(i));  
            }  
        }
    	System.out.println(termlist);
    	model.addAttribute("termlist", termlist);
        model.addAttribute("perm", request.getSession().getAttribute("perm"));
        model.addAttribute("name", request.getSession().getAttribute("name"));
        model.addAttribute("grade", request.getSession().getAttribute("grade"));
        model.addAttribute("class_num", request.getSession().getAttribute("class_num"));
        return "/html/examresult";
    }
    
    @RequestMapping(value="/resultsearch", method=RequestMethod.GET)
    @ResponseBody
	public Map<String, List<Map<String, Object>>> search(HttpServletRequest request, @RequestParam(value = "semester", required = true) String semester, @RequestParam(value = "examtype", required = true) String examtype, @RequestParam(value = "grades", required = true) String grades, @RequestParam(value = "classes", required = true) String classes, @RequestParam(value = "stuname", required = true) String stuname, @RequestParam(value = "limit", required = true) int limit, @RequestParam(value = "offset", required = true) int offset) {
		Map<String, List<Map<String, Object>>> res = new HashMap<String, List<Map<String, Object>>>();
		res.put("data", ResultService.getUserListByCondition(semester, examtype, grades, classes, stuname, limit, offset));
		res.put("alldata", ResultService.getAllresult(semester, examtype, grades, classes, stuname));
        res.put("count", ResultService.getCount(semester, examtype, grades, classes, stuname));
		return res;
	}
}
